﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.API.Application.Commands.MaterialManagements;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.BoM
{
    /// <summary>
    /// 根据物料ID获取物料信息Handler
    /// </summary>
    public class SearchMaterialInforHandler : IRequestHandler<SearchMaterialInforCommand, Result<MaterialDto>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 物料管理表仓储
        /// </summary>
        public readonly IBaseRepository<MaterialManagement> materialManagementrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchMaterialInforHandler> logger;
        /// <summary>
        /// 物料类别表仓储
        /// </summary>
        public readonly IBaseRepository<MaterialCategory> materialCategoryrepository;
        /// <summary>
        /// 物料服务表仓储
        /// </summary>
        public readonly IBaseRepository<MaterialsServices> materialsServicesrepository;
        /// <summary>
        /// 物料单位表仓储
        /// </summary>
        public readonly IBaseRepository<MaterialUnit> materialUnitrepository;
        /// <summary>
        /// 规格单位表仓储
        /// </summary>
        public readonly IBaseRepository<SpecificationUnit> specificationUnitrepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="mapper"></param>
        /// <param name="materialManagementrepository"></param>
        /// <param name="logger"></param>
        /// <param name="materialCategoryrepository"></param>
        /// <param name="materialsServicesrepository"></param>
        /// <param name="materialUnitrepository"></param>
        /// <param name="specificationUnitrepository"></param>
        public SearchMaterialInforHandler(ISqlSugarClient db, IMapper mapper, IBaseRepository<MaterialManagement> materialManagementrepository, ILogger<SearchMaterialInforHandler> logger, IBaseRepository<MaterialCategory> materialCategoryrepository, IBaseRepository<MaterialsServices> materialsServicesrepository, IBaseRepository<MaterialUnit> materialUnitrepository, IBaseRepository<SpecificationUnit> specificationUnitrepository)
        {
            _db = db;
            this.mapper = mapper;
            this.materialManagementrepository = materialManagementrepository;
            this.logger = logger;
            this.materialCategoryrepository = materialCategoryrepository;
            this.materialsServicesrepository = materialsServicesrepository;
            this.materialUnitrepository = materialUnitrepository;
            this.specificationUnitrepository = specificationUnitrepository;
        }
        /// <summary>
        /// 根据物料ID获取物料信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<MaterialDto>> Handle(SearchMaterialInforCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<MaterialDto>() { Code = (int)CommonErrorCode.Success };
            try
            {
                var query = await _db.Queryable<MaterialManagement, MaterialCategory, MaterialsServices, MaterialUnit, SpecificationUnit>((a, b, c, d, e) => new JoinQueryInfos(
                    JoinType.Inner, a.MaterialCategoryId == b.MaterialCategoryId,
                    JoinType.Inner, a.MaterialsServicesId == c.MaterialsServicesId,
                    JoinType.Inner, a.MaterialUnitId == d.MaterialUnitId,
                    JoinType.Inner, a.SpecificationUnitId == e.SpecificationUnitId
                    ))
                    .Select((a, b, c, d, e) => new MaterialDto()
                    {
                        MaterialId = a.MaterialId,
                        MaterialName = a.MaterialName,
                        MaterialCode = a.MaterialCode,
                        Specifications = a.Specifications,
                        MaterialCategoryId = b.MaterialCategoryId,
                        MaterialCategoryName = b.MaterialCategoryName,
                        MaterialsServicesId = c.MaterialsServicesId,
                        MaterialsServicesName = c.MaterialsServicesName,
                        MaterialUnitId = d.MaterialUnitId,
                        MaterialUnitName = d.MaterialUnitName,
                        SpecificationUnitId = e.SpecificationUnitId,
                        SpecificationUnitName = e.SpecificationUnitName,
                        PackagingSpecifications = a.PackagingSpecifications,
                        InventoryLimit = a.InventoryLimit,
                        InventoryLowerLimit = a.InventoryLowerLimit,
                        IsStatus = a.IsStatus,
                        IsDel = a.IsDel,
                        AuditStatus = a.AuditStatus,
                        CreatedOwner = a.CreatedOwner,
                        CreatedTime = a.CreatedTime
                    }).Where(a => a.IsDel == 1).FirstAsync(a => a.MaterialId == request.MaterialId);
                result.Data = query;
                result.Msg = "根据物料ID获取物料信息查询成功";
            }
            catch (Exception ex)
            {
                result.Msg = "根据物料ID获取物料信息查询失败" + ex.Message;
                logger.LogError("根据物料ID获取物料信息查询失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
